package com.app.service.Impl;

import com.app.mapper.*;
import com.app.model.*;
import com.app.service.ActivityAdminService;
import com.app.service.ActivityService;
import com.app.service.AreaService;
import com.app.util.CommonHelp;
import com.app.vo.ActivityEnlistVo;
import com.app.vo.ActivityVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ActivityAdminServiceImpl implements ActivityAdminService {
    @Autowired
    private ActivityAdminMapper activityAdminMapper;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityEnlistMapper activityEnlistMapper;
    @Autowired
    private UserIntegralMapper userIntegralMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AreaService areaService;

    @Override
    public void insert(ActivityModel activity) {
        if (activity.getStartTime() != null && activity.getEndTime() != null) {
            String startTime = activity.getStartTime();
            String endTime = activity.getEndTime();
            activity.setStartTime(startTime + " 00:00:00");
            activity.setEndTime(endTime + " 23:59:59");
        }
        activity.setCreateTime(CommonHelp.getNowTimestamp());
        activityAdminMapper.insert(activity);
    }

    @Override
    public void updateById(ActivityModel activity) {
        if (activity.getStartTime() != null && activity.getEndTime() != null) {
            String startTime = activity.getStartTime();
            String endTime = activity.getEndTime();
            activity.setStartTime(startTime + " 00:00:00");
            activity.setEndTime(endTime + " 23:59:59");
        }
        activity.setUpdateTime(CommonHelp.getNowTimestamp());
        activityAdminMapper.updateById(activity);
    }

    @Override
    public void deleteActivityById(Integer id) {
        ActivityModel activity = new ActivityModel();
        activity.setId(id);
        activity.setStates(0);
        activityAdminMapper.updateById(activity);
    }

    @Override
    public ActivityVo selectActivityList(Integer page, Integer limit, String title, Integer villageId) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//设置格式
        if (page == null || limit == null) {
            return null;
        }
        page = (page - 1) * limit;
        List<ActivityModel> activityModels = activityAdminMapper.selectActivityList(page, limit, title, villageId);
        for (int i = 0; i < activityModels.size(); i++) {
            if (activityModels.get(i).getVillageId() != 0) {
                AreaModel villageAreaModel = areaService.selectById(activityModels.get(i).getVillageId());
                AreaModel townAreaModel = areaService.selectById(villageAreaModel.getPid());
                AreaModel cityAreaModel = areaService.selectById(townAreaModel.getPid());
                activityModels.get(i).setVillageText(cityAreaModel.getName() + "/" + townAreaModel.getName() + "/" + villageAreaModel.getName());
            } else {
                activityModels.get(i).setVillageText("");
            }
        }
        List<SystemModel> systemModels = activityService.selectTypeList();
        for (int i = 0; i < activityModels.size(); i++) {
            for (int i1 = 0; i1 < systemModels.size(); i1++) {
                if (activityModels.get(i).getType().equals(systemModels.get(i1).getId())) {
                    activityModels.get(i).setTypeText(systemModels.get(i1).getName());
                }
            }
        }
        for (int i = 0; i < activityModels.size(); i++) {
            Date parse = simpleDateFormat.parse(activityModels.get(i).getStartTime());
            Date parse1 = simpleDateFormat.parse(activityModels.get(i).getEndTime());
            activityModels.get(i).setStartTime(simpleDateFormat.format(parse));
            activityModels.get(i).setEndTime(simpleDateFormat.format(parse1));
        }
        Integer count = null;
        QueryWrapper<ActivityModel> queryWrapper = new QueryWrapper<>();
        if (villageId != null && villageId != 0) {
            queryWrapper.eq("village_id", villageId);
            count = activityAdminMapper.selectCount(queryWrapper);
        } else {
            count = activityAdminMapper.selectCount(null);
        }
        ActivityVo vo = new ActivityVo();
        vo.setList(activityModels);
        vo.setCount(count);
        return vo;
    }

    @Override
    public List<ActivityModel> selectApplicationTitle(Integer villageId) {
        return activityAdminMapper.selectApplicationTitle(villageId);
    }

    @Override
    public ActivityEnlistVo selectApplication(Integer page, Integer limit, String keyword, Integer activityId, Integer villageId) {
        ActivityEnlistModel model = new ActivityEnlistModel();
        model.setPage((page - 1) * limit);
        model.setLimit(limit);
        model.setKeyword(keyword);
        model.setActivityId(activityId);
        List<ActivityEnlistModel> activityEnlistModels = activityAdminMapper.selectApplication(model);
        for (int i = 0; i < activityEnlistModels.size(); i++) {
            if (activityEnlistModels.get(i).getFiles() == null || activityEnlistModels.get(i).getFiles().equals("")) {
                String[] str = new String[0];
                activityEnlistModels.get(i).setFilesArr(str);
            }
            if (activityEnlistModels.get(i).getFiles() != null) {
                String[] str = activityEnlistModels.get(i).getFiles().split(",");
                activityEnlistModels.get(i).setFilesArr(str);
            }
        }
        for (int i = 0; i < activityEnlistModels.size(); i++) {
            if (activityEnlistModels.get(i).getAuditStatus() == 0) {
                activityEnlistModels.get(i).setAuditStatusText("待审核");
            }
            if (activityEnlistModels.get(i).getAuditStatus() == 1) {
                activityEnlistModels.get(i).setAuditStatusText("通过");
            }
            if (activityEnlistModels.get(i).getAuditStatus() == 2) {
                activityEnlistModels.get(i).setAuditStatusText("拒绝");
            }
        }
        for (int i = 0; i < activityEnlistModels.size(); i++) {
            ActivityModel model1 = activityAdminMapper.selectById(activityEnlistModels.get(i).getActivityId());
            if (model1 != null) {
                activityEnlistModels.get(i).setActivityText(model1.getTitle());
            }
        }
        for (int i = 0; i < activityEnlistModels.size(); i++) {
            if (activityEnlistModels.get(i).getStatus() == 0) {
                activityEnlistModels.get(i).setStatusText("待审核");
            }
            if (activityEnlistModels.get(i).getStatus() == 1) {
                activityEnlistModels.get(i).setStatusText("通过");
            }
            if (activityEnlistModels.get(i).getStatus() == 2) {
                activityEnlistModels.get(i).setStatusText("拒绝");
            }
        }
        ActivityEnlistVo vo = new ActivityEnlistVo();
        if (villageId != null && villageId != 0) {
            List<ActivityEnlistModel> activityEnlistModelList = new ArrayList<>();
            for (int i = 0; i < activityEnlistModels.size(); i++) {
                UserModel model1 = userMapper.selectById(activityEnlistModels.get(i).getUserId());
                if (model1 != null && model1.getVillageId().equals(villageId)) {
                    activityEnlistModelList.add(activityEnlistModels.get(i));
                }
            }
            vo.setList(activityEnlistModelList);
            vo.setCount(activityEnlistModelList.size());
            return vo;
        }
        Integer count = activityAdminMapper.selectApplicationCount(keyword, activityId);
        vo.setList(activityEnlistModels);
        vo.setCount(count);
        return vo;
    }

    @Override
    public ActivityEnlistModel examine(ActivityEnlistModel model) {
        model.setUpdateTime(CommonHelp.getNowTimestamp());
        activityEnlistMapper.updateById(model);
        ActivityEnlistModel model1 = activityEnlistMapper.selectById(model.getId());
        if (model1 != null) {
            if (model1.getAuditStatus() == 1) {
                //获取积分规则
                ActivityModel activityModel = activityMapper.selectById(model1.getActivityId());
                UserIntegralModel userIntegralModel = new UserIntegralModel();
                userIntegralModel.setUserId(model1.getUserId());
                userIntegralModel.setType(1);
                userIntegralModel.setIntegral(activityModel.getIntegral());
                userIntegralModel.setAssociatedId(model.getId());
                userIntegralModel.setCreateTime(CommonHelp.getNowTimestamp());
                userIntegralModel.setStatus(1);
                UserModel userModel = userMapper.selectById(model1.getUserId());
                if (userIntegralModel.getAfterIntegral() == null) {
                    userIntegralModel.setAfterIntegral(userModel.getIntegral());
                }
                userIntegralModel.setAfterIntegral(userIntegralModel.getAfterIntegral() + userIntegralModel.getIntegral());
                userIntegralMapper.insert(userIntegralModel);
                if (userIntegralModel.getAfterIntegral() != null) {
                    userModel.setIntegral(userIntegralModel.getAfterIntegral());
                }
                userModel.setUpdateTime(CommonHelp.getNowTimestamp());
                userMapper.updateById(userModel);
            }
        }
        QueryWrapper<ActivityEnlistModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", model.getId());
        return activityEnlistMapper.selectOne(queryWrapper);
    }


    @Override
    public void ImageExamine(Integer auditStatus, Integer id, String auditRemark) {
        ActivityEnlistModel model = activityEnlistMapper.selectById(id);
        model.setAuditStatus(auditStatus);
        model.setAuditRemark(auditRemark);
        activityEnlistMapper.updateById(model);
        if (auditStatus == 1) {
            //获取积分规则
            ActivityModel activityModel = activityMapper.selectById(model.getActivityId());
            UserIntegralModel userIntegralModel = new UserIntegralModel();
            userIntegralModel.setUserId(id);
            userIntegralModel.setType(1);
            userIntegralModel.setIntegral(activityModel.getIntegral());
            userIntegralModel.setAssociatedId(model.getId());
            userIntegralModel.setCreateTime(CommonHelp.getNowTimestamp());
            userIntegralModel.setAfterIntegral(userIntegralModel.getAfterIntegral() + userIntegralModel.getIntegral());
            userIntegralMapper.insert(userIntegralModel);
            UserModel userModel = userMapper.selectById(model.getUserId());
            userModel.setIntegral(userIntegralModel.getAfterIntegral());
            userModel.setUpdateTime(CommonHelp.getNowTimestamp());
            userMapper.updateById(userModel);
        }
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        ActivityModel model = new ActivityModel();
        model.setId(id);
        model.setStatus(status);
        activityAdminMapper.updateById(model);
    }
}
